﻿using SharpSvn;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VteamProjectAssistant.SettingWindow;

namespace VteamProjectAssistant
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        /// <summary>
        /// true 上锁 false 解锁
        /// </summary>
        private bool _lockStatus;

        private MainWindowViewModel _mainWindowViewModel;

        private readonly SettingWindowView _settingWindowView;

        public MainWindow(MainWindowViewModel mainWindowViewModel, SettingWindowView settingWindowView)
        {
            InitializeComponent();
            DataContext = mainWindowViewModel;
            _mainWindowViewModel = mainWindowViewModel;
            _settingWindowView = settingWindowView;

        }

        private void DirectoryTree_Selected(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is TreeViewItem item)
            {
                Console.Write(item);
            }
        }

        private void Svn_Update_Button_Click(object sender, RoutedEventArgs e)
        {
            using (SvnClient client = new SvnClient())
            {
                string path = _mainWindowViewModel!.ExcelFilePath!;
                SvnUpdateArgs updateArgs = new SvnUpdateArgs();
                updateArgs.Depth = SvnDepth.Empty;
                client.Update(path, updateArgs);
                MessageBox.Show("更新成功");
            }
        }

        private void Svn_Commit_Button_Click(Object sender, RoutedEventArgs e)
        {
            using (SvnClient client = new SvnClient())
            {
                string path = _mainWindowViewModel!.ExcelFilePath!;
                SvnCommitArgs commitArgs = new SvnCommitArgs();
                commitArgs.Depth = SvnDepth.Empty;
                commitArgs.LogMessage = "My Test Commit";
                SvnCommitResult commitResult = null;
                client.Commit(path, commitArgs, out commitResult);
                MessageBox.Show("提交成功");
            }
        }


        private void Svn_Mode_Button_Click(Object sender, RoutedEventArgs e)
        {
            bool isLockedByMySelf = false;
            if (_lockStatus)
            {
                modeButton.Content = "加锁";
                this.Svn_Unlock_Button_Click(sender, e);
            }
            else
            {
                modeButton.Content = "解锁";
                this.Svn_Lock_Button_Click(sender, e);
            }
            _lockStatus = !_lockStatus;
            _mainWindowViewModel!.EditMode = _lockStatus ? "只读" : "编辑";
        }

        private void Svn_Lock_Button_Click(Object sender, RoutedEventArgs e)
        {
            using (SvnClient client = new SvnClient())
            {
                string path = _mainWindowViewModel!.ExcelFilePath!;
                SvnLockArgs lockArgs = new SvnLockArgs();
                lockArgs.Comment = "Test Lock";
                client.Lock(path, lockArgs);
                MessageBox.Show("上锁成功");
            }
        }

        private void Svn_Unlock_Button_Click(Object sender, RoutedEventArgs e)
        {
            using (SvnClient client = new SvnClient())
            {
                string path = _mainWindowViewModel!.ExcelFilePath!;
                SvnUnlockArgs unlockArgs = new SvnUnlockArgs();
                client.Unlock(path, unlockArgs);
                MessageBox.Show("解锁成功");
            }
        }

        private void Setting_Click_Event(object sender, RoutedEventArgs e)
        {
            _settingWindowView.ShowDialog();
        }
    }
}